<?php


namespace app\admin\controller\v1;


use app\admin\model\StatisticsModel;
use app\admin\service\TimeTaskService;
use app\BaseController;
use app\utils\redis\RedisCache;
use app\utils\redis\RedisKey;
use app\utils\redis\RedisObj;
use think\App;
use think\facade\Env;

/**
 * blog定时任务
 * Class TimeTask
 * @package app\blog\controller\v1
 */
class TimeTask extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        if($this->request->ip()!=Env::get('task.ip')){
            throw new \Exception('权限错误');
        }
    }

    /**
     * 定时任务，每天12自动统计，并重置
     * @return \think\response\Json
     */
    public function cleanAdAndArticleTodayClickNum() {
        $service = new TimeTaskService();
        $ad_arr = $service->cleanArticleAndIdTodayIpNum(RedisKey::$adReadNumKey);
        $article_arr = $service->cleanArticleAndIdTodayIpNum(RedisKey::$articleReadNumKey);
        $statisticsModel = new StatisticsModel();
        $bool = $statisticsModel->save(
            [
                'today_article_num' => json_encode($article_arr),
                'today_ad_num' => json_encode($ad_arr),
                'add_time' => getDateTime(),
            ]
        );
        if(!$bool){
            return errorJson(80001, '统计失败');
        }
        return successJson('success');
    }


}